"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A4检测项确保配置密码的有效期限检测与修复成功
Create At   : 2024/10/25
Owner       : lihongji
Description :
    1.设置password_effect_time参数为90后检测A4项
    2.对正确password_effect_time配置修复后在数据库中查看其值
    3.设置password_effect_time参数为0后检测A4项
    4.修复A4不符合安全规范项目后再次检测
    5.登入数据库查看password_effect_time的值
    6.设置password_effect_time参数为999后检测A4项
    7.修复A4符合安全规范项目后查看password_effect_time的值
Expect      :
    1.设置成功，不打印password_effect_time告警信息
    2.修复成功，password_effect_time为90
    3.设置成功，打印password_effect_time告警信息
    4.不打印password_effect_time告警信息
    5.password_effect_time参数的值为90
    6.设置成功，不打印password_effect_time告警信息
    7.password_effect_time参数的值为999
History     :
"""
import os
import time
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):

        step = ('----step1:设置password_effect_time参数为90后检测A4项'
                'expect:设置成功，不打印password_effect_time告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload ', self.constant.GSGUC_SUCCESS_MSG,
            "password_effect_time=90")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A4 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(excute_msg)
        self.assertNotIn("Ensure configuration of password expiration"
                         " periods.", check_res, '执行失败' + step)

        step = ('----step2:对正确password_effect_time配置修复后在数据库中查看其值'
                'expect:修复成功，password_effect_time为90----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B4 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show password_effect_time"
        sql_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(sql_res)
        self.assertIn('90', sql_res, '执行失败' + step)
        self.assertIn("Setting Account Password Management succeed.",
                      check_res, '执行失败' + step)

        step = ('----step3:设置password_effect_time参数为0后检测A4项  '
                'expect:设置成功，打印password_effect_time告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload ', self.constant.GSGUC_SUCCESS_MSG,
            "password_effect_time=0")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A4 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(excute_msg)
        self.assertIn("Ensure configuration of password expiration "
                      "periods.", check_res, '执行失败' + step)

        step = ('----step4:修复A4不符合安全规范项目后再次检测 '
                'expect:不打印password_effect_time告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B4 --detail;'
                          f'gs_checkse -i A4 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(excute_msg)
        self.assertIn("Setting Account Password Management succeed",
                      check_res, '执行失败' + step)
        self.assertNotIn("Ensure configuration of password expiration"
                         " periods.", check_res, '执行失败' + step)

        step = ('----step5:登入数据库查看password_effect_time的值'
                'expect:修复成功，password_effect_time为1d----')
        self.log.info(step)
        check_sql = "show password_effect_time"
        sql_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(sql_res)
        self.assertIn('90', sql_res, '执行失败' + step)

        step = ('----step6:设置password_effect_time参数为999后检测A4项  '
                'expect:设置成功，不打印password_effect_time告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload ', self.constant.GSGUC_SUCCESS_MSG,
            "password_effect_time=999")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A4 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(excute_msg)
        self.assertNotIn("Ensure configuration of password expiration"
                         " periods.", check_res, '执行失败' + step)

        step = ('----step7:修复A4符合安全规范项目后查看password_effect_time的值'
                'expect:修复成功，password_effect_time值为999----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B4 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show password_effect_time"
        sql_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(sql_res)
        self.assertIn('999', sql_res, '执行失败' + step)
        self.assertIn("Setting Account Password Management succeed",
                      check_res, '执行失败' + step)

    def tearDown(self):
        step = '----step8:清理环境 expect:成功----'
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            "password_effect_time=90")
        self.assertTrue(excute_msg)
        self.log.info(f'----{os.path.basename(__file__)} end----')